﻿using System.Reflection;

namespace Songhay.Wpf.Mvvm.ViewModels
{
    using Songhay.Models;

    /// <summary>
    /// Models the “About…” dialog data.
    /// </summary>
    public class AboutViewModel : ViewModelBase, IFrameworkAssemblyInfo
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="AboutViewModel"/> class.
        /// </summary>
        public AboutViewModel()
        {
            if(base.IsInDesignMode)
            {
                this.SetProperties(Assembly.GetExecutingAssembly());
            }
            else
            {
                var assembly = Assembly.GetEntryAssembly();
                if(assembly == null) return; //FUNKYKB: VSTO app domain returns null.
                this.SetProperties(assembly);
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AboutViewModel"/> class.
        /// </summary>
        /// <param name="dll">The DLL.</param>
        public AboutViewModel(Assembly dll)
        {
            this.SetProperties(dll);
        }

        #region IFrameworkAssemblyInfo Members

        /// <summary>
        /// Gets or sets the assembly company.
        /// </summary>
        /// <value>The assembly company.</value>
        public string AssemblyCompany
        {
            get
            {
                return _assemblyCompany;
            }
            set
            {
                _assemblyCompany = value;
                base.RaisePropertyChanged("AssemblyCompany");
            }
        }

        /// <summary>
        /// Gets or sets the assembly copyright.
        /// </summary>
        /// <value>The assembly copyright.</value>
        public string AssemblyCopyright
        {
            get
            {
                return _assemblyCopyright;
            }
            set
            {
                _assemblyCopyright = value;
                base.RaisePropertyChanged("AssemblyCopyright");
            }
        }

        /// <summary>
        /// Gets or sets the assembly description.
        /// </summary>
        /// <value>The assembly description.</value>
        public string AssemblyDescription
        {
            get
            {
                return _assemblyDescription;
            }
            set
            {
                _assemblyDescription = value;
                base.RaisePropertyChanged("AssemblyDescription");
            }
        }

        /// <summary>
        /// Gets or sets the assembly product.
        /// </summary>
        /// <value>The assembly product.</value>
        public string AssemblyProduct
        {
            get
            {
                return _assemblyProduct;
            }
            set
            {
                _assemblyProduct = value;
                base.RaisePropertyChanged("AssemblyProduct");
            }
        }

        /// <summary>
        /// Gets or sets the assembly title.
        /// </summary>
        /// <value>The assembly title.</value>
        public string AssemblyTitle
        {
            get
            {
                return _assemblyTitle;
            }
            set
            {
                _assemblyTitle = value;
                base.RaisePropertyChanged("AssemblyTitle");
            }
        }

        /// <summary>
        /// Gets or sets the assembly version.
        /// </summary>
        /// <value>The assembly version.</value>
        public string AssemblyVersion
        {
            get
            {
                return _assemblyVersion;
            }
            set
            {
                _assemblyVersion = value;
                base.RaisePropertyChanged("AssemblyVersion");
            }
        }

        /// <summary>
        /// Gets or sets the assembly version detail.
        /// </summary>
        /// <value>The assembly version detail.</value>
        public string AssemblyVersionDetail
        {
            get
            {
                return _assemblyVersionDetail;
            }
            set
            {
                _assemblyVersionDetail = value;
                base.RaisePropertyChanged("AssemblyVersionDetail");
            }
        }

        #endregion

        void SetProperties(Assembly dll)
        {
            var data = new FrameworkAssemblyInfo(dll);
            FrameworkTypeUtility.SetProperties<FrameworkAssemblyInfo, AboutViewModel>(data, this);
            base.CommandDictionary.Add("#about-uri", () =>
            {
                var location = string.Concat("http://songhaysystem.com/document/code/", dll.GetName().Name);
                FrameworkUtility.StartProcess(location, string.Empty, false);
            });
        }

        private string _assemblyCompany;
        private string _assemblyCopyright;
        private string _assemblyDescription;
        private string _assemblyProduct;
        private string _assemblyTitle;
        private string _assemblyVersion;
        private string _assemblyVersionDetail;
    }
}
